package com.hexb.smh.controller.management;

import com.hexb.core.utils.Assert;
import com.hexb.core.utils.ObjectUtils;
import com.hexb.smh.annotations.AopOperationLog;
import com.hexb.smh.entity.Brand;
import com.hexb.smh.entity.Config;
import com.hexb.smh.entity.enums.ConfigType;
import com.hexb.smh.entity.enums.Operators;
import com.hexb.smh.entity.param.account.ClientLoginParam;
import com.hexb.smh.entity.param.config.ConfigParam;
import com.hexb.smh.entity.param.config.EvaluateParam;
import com.hexb.smh.entity.view.EvaluateResult;
import com.hexb.smh.errors.ErrorCodes;
import com.hexb.smh.service.IConfigService;
import com.hexb.smh.service.IEvaluateService;
import com.hexb.smh.utils.AccountHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static com.hexb.smh.utils.SmhConstant.*;

/**
 * @author : hexb
 */
@RestController
@RequestMapping("/management/config")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "A5.系统配置", description = "Server", tags = {"A5.系统配置"})
@Slf4j
public class ConfigController {

    @NonNull
    final private IConfigService configService;

    @NonNull
    final private IEvaluateService evaluateService;


    @ApiOperation("保存配置")
    @AopOperationLog(name = "保存配置", operations = Operators.saveConfig)
    @RequiresRoles(value = {ROLE_SUPER_ADMIN, ROLE_ADMIN}, logical = Logical.OR)
    @PostMapping
    public int append(@RequestBody @Valid ConfigParam param) {
        Config config = param.toConfig();
        Integer id = AccountHelper.getAccount().getId();
        config.setCreateBy(id);
        config.setUpdateBy(id);
        return configService.saveOrUpdate(config);
    }

    @ApiOperation("获取配置列表")
    @RequiresRoles(value = {ROLE_SUPER_ADMIN, ROLE_ADMIN}, logical = Logical.OR)
    @GetMapping("/map")
    public Map<ConfigType, Config> map() {
        List<Config> list = configService.list();
        return ObjectUtils.list2map(list, Config::getType);
    }


    @ApiOperation("验证公式是否合法")
    @RequiresRoles(value = {ROLE_SUPER_ADMIN, ROLE_ADMIN}, logical = Logical.OR)
    @GetMapping("/expression/valid")
    public Boolean validExpression(String ex) {
        try {
            String s = configService.validExpression(ex, null);
            return !StringUtils.isEmpty(s);
        } catch (Exception e) {
            log.error("公式测试发生错误", e);
        }
        return false;
    }


    @ApiOperation("模拟评估测试配置")
    @AopOperationLog(name = "提交套餐评估(测试)", operations = Operators.evaluate)
    @RequiresRoles(value = {ROLE_SUPER_ADMIN, ROLE_ADMIN}, logical = Logical.OR)
    @PostMapping("/evaluate")
    public EvaluateResult evaluate(@RequestBody @Valid EvaluateParam param) {
        return evaluateService.evaluate(
                param.getIds(),
                param.getDealPrice(),
                param.getBrandId() == null ? Brand.DEFAULT : param.getBrandId());
    }

}
